package com.panfeng.xcloud.scheduler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.binance.client.model.enums.CandlestickInterval;
import com.huobi.api.enums.DirectionEnum;
import com.panfeng.xcloud.boss.provider.dto.request.PinBarCandlerStickDetailDTO;
import com.panfeng.xcloud.boss.provider.dto.request.QuantBotDetailDTO;
import com.panfeng.xcloud.boss.provider.dto.request.QuantCrossCandlerStickDetailDTO;
import com.panfeng.xcloud.common.core.candle.Entry;
import com.panfeng.xcloud.common.core.candle.MyCandleEntry;
import com.panfeng.xcloud.common.core.enums.*;
import com.panfeng.xcloud.common.core.exceptions.BaseBizException;
import com.panfeng.xcloud.scheduler.constants.SystemParamContants;
import com.panfeng.xcloud.scheduler.service.ICalculatePriceService;
import com.panfeng.xcloud.scheduler.service.IFeignService;
import com.panfeng.xcloud.scheduler.service.ITechniqueSymbolService4BackTest;
import com.panfeng.xcloud.scheduler.util.LineChartUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.ta4j.core.BarSeries;
import org.ta4j.core.num.Num;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

@SpringBootTest
//@SpringBootTest(classes = SchedulerTest.class)
@RunWith(SpringRunner.class)
@Slf4j
public class SchedulerEmaCrossTest2 {

    @Autowired
    private IFeignService feignService;

    @Autowired
    private ITechniqueSymbolService4BackTest iTechniqueSymbolService4BackTest;

    @Autowired
    private ICalculatePriceService iCalculatePriceService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /*JSONArray candleStickAllTime = new JSONArray();
    JSONObject candleStickObjNow = new JSONObject();
    JSONArray candleStickArrayNow = new JSONArray();
    QuantBotDetailDTO buyQuantBotDetailDTO = new QuantBotDetailDTO();
    QuantBotDetailDTO sellQuantBotDetailDTO = new QuantBotDetailDTO();*/

    HashMap<String, JSONArray> candleStickAllTimeMap = new HashMap<>();
    HashMap<String, JSONObject> candleStickObjNowMap = new HashMap<>();
    HashMap<String, JSONArray> candleStickArrayNowMap = new HashMap<>();
    HashMap<String, QuantBotDetailDTO> buyQuantBotDetailDTOMap = new HashMap<>();
    HashMap<String, QuantBotDetailDTO> sellQuantBotDetailDTOMap = new HashMap<>();

    int buyWinStreakCount = 0;
    int buyWinStreakMaxCount = 0;
    int buyStopStreakMaxCount = 0;
    String buyWinStreakMaxCountTime = "0";
    String buyStopStreakMaxCountTime = "0";

    int sellWinStreakCount = 0;
    int sellWinStreakMaxCount = 0;
    int sellStopStreakMaxCount = 0;
    String sellWinStreakMaxCountTime = "0";
    String sellStopStreakMaxCountTime = "0";

    int winStreakCount = 0;
    int stopStreakCount = 0;
    int winStreakMaxCount = 0;
    int stopStreakMaxCount = 0;
    String winStreakMaxCountTime = "0";
    String stopStreakMaxCountTime = "0";


    int transferCounts = 0;
    int winCounts = 0;
    BigDecimal thisCircleMoneyChange = BigDecimal.ZERO;
    BigDecimal leverageRatio = new BigDecimal("10");

    List<ZonedDateTime> xTime = new ArrayList<>();
    List<Double> yWinRate = new ArrayList<>();
    List<Double> yAvaliableMoney = new ArrayList<>();

    BigDecimal winMoney = BigDecimal.ZERO;
    BigDecimal lossMoney = BigDecimal.ZERO;
    BigDecimal winLossMoneyRatio = BigDecimal.ZERO;

    BigDecimal initStopLossRateMax4Test = BigDecimal.ZERO;
    List<BigDecimal> initStopLossRateMaxList4Test = new ArrayList<>();

    @Test
    public void testAll() throws Exception {
        String fromString = "2024-01-01 00:01";
        String toString = "2024-07-12 20:00";

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        LocalDateTime fromDate = LocalDateTime.parse(fromString, formatter);
        LocalDateTime toDate = LocalDateTime.parse(toString, formatter);

        LocalDateTime currentStart = fromDate;
        LocalDateTime currentEnd;

        while (currentStart.isBefore(toDate)) {
            currentEnd = currentStart.plusMonths(1).plusMinutes(5);

            if (currentEnd.isAfter(toDate)) {
                currentEnd = toDate;
            }

            String dateRange = currentStart.format(formatter) + " - " + currentEnd.format(formatter);
            System.out.println(dateRange);

            //test(currentStart.format(formatter), currentEnd.format(formatter));

            candleStickAllTimeMap.clear();
            candleStickObjNowMap.clear();
            candleStickArrayNowMap.clear();
            buyQuantBotDetailDTOMap.clear();
            sellQuantBotDetailDTOMap.clear();
            transferCounts = 0;
            winCounts = 0;
            thisCircleMoneyChange = BigDecimal.ZERO;
            xTime.clear();
            yWinRate.clear();
            yAvaliableMoney.clear();

            currentStart = currentStart.plusMonths(1);
        }
    }

    @Test
    public void test() throws Exception {
        String userId = "b60000";
        String destinationCoins = "ONT";
        String openMoney = "25245";
        double initMoney = 5049;
        String[] destinationCoinArray = destinationCoins.split(",");
        for (String destinationCoin : destinationCoinArray) {
            toSetQuantBotDetail(userId, destinationCoin, openMoney);
        }

        String fromString = "2024-08-07 00:10";
        String toString = "2024-08-18 19:30";
        Long from = getTimeStamp(fromString);
        Long to = getTimeStamp(toString);

        xTime.add(ZonedDateTime.ofInstant(Instant.ofEpochMilli(from), ZoneId.systemDefault()));
        yWinRate.add(0.0);
        yAvaliableMoney.add(initMoney);
        redisTemplate.opsForValue().set("initStopMax:" + destinationCoins, BigDecimal.ZERO);
        redisTemplate.opsForValue().set("initStopMaxList:" + destinationCoins, new ArrayList<BigDecimal>());

        long stime = System.currentTimeMillis();
        CandlestickInterval interval = CandlestickInterval.FIFTEEN_MINUTES;
        Duration barSeriesTimePeriod = Duration.ofMinutes(15);
        for (String destinationCoin : destinationCoinArray) {
            getCandleStickInit(from, interval, destinationCoin);
            getCandleStickAllTime(from, to, interval, destinationCoin);
        }

        JSONArray allTimes = new ArrayList<>(candleStickAllTimeMap.values()).get(0);
        for (int i = 0; i < allTimes.size(); i++) {
            BarSeries barSeries = null;
            thisCircleMoneyChange = BigDecimal.ZERO;
            for (String destinationCoin : destinationCoinArray) {
                getCandleStickNow(i, destinationCoin);

                QuantBotDetailDTO buyQuantBotDetailDTO = buyQuantBotDetailDTOMap.get(destinationCoin);
                QuantBotDetailDTO sellQuantBotDetailDTO = sellQuantBotDetailDTOMap.get(destinationCoin);

                JSONObject candleStickObjNow = candleStickObjNowMap.get(destinationCoin);
                List<MyCandleEntry> candleEntries = iTechniqueSymbolService4BackTest.getCandlerStick(candleStickObjNow);
                barSeries = iTechniqueSymbolService4BackTest.getBarSeries(candleEntries, barSeriesTimePeriod);

                buyQuantBotDetailDTO = robotExecute(buyQuantBotDetailDTO, barSeries, destinationCoin, destinationCoins);
                sellQuantBotDetailDTO = robotExecute(sellQuantBotDetailDTO, barSeries, destinationCoin, destinationCoins);

                buyQuantBotDetailDTOMap.put(destinationCoin, buyQuantBotDetailDTO);
                sellQuantBotDetailDTOMap.put(destinationCoin, sellQuantBotDetailDTO);
            }
            xTime.add(barSeries.getBar(barSeries.getEndIndex()).getEndTime());
            if (transferCounts == 0) {
                yWinRate.add(0.0);
            } else {
                yWinRate.add(new BigDecimal(winCounts).divide(new BigDecimal(transferCounts), 6, BigDecimal.ROUND_DOWN).multiply(new BigDecimal("100")).doubleValue());
            }
            BigDecimal current = BigDecimal.valueOf(yAvaliableMoney.get(yAvaliableMoney.size() - 1)).add(thisCircleMoneyChange);
            yAvaliableMoney.add(current.doubleValue());
        }
        long etime = System.currentTimeMillis();
        long needTime = etime - stime;
        System.out.println("执行时间:" + convertMillisecondsToHMS(needTime));
        System.out.println("总开单数量:" + transferCounts + ",胜场数量:" + winCounts);
        System.out.println("多单最大连盈时间: " + buyWinStreakMaxCountTime + "多单最大连盈次数:" + buyWinStreakMaxCount + ",多单最大连亏时间:" + buyStopStreakMaxCountTime + ",多单最大连亏次数:" + buyStopStreakMaxCount);
        System.out.println("空单最大连盈时间: " + sellWinStreakMaxCountTime + "空单最大连盈次数:" + sellWinStreakMaxCount + ",空单最大连亏时间:" + sellStopStreakMaxCountTime + ",空单最大连亏次数:" + sellStopStreakMaxCount);
        System.out.println("总共最大连盈时间: " + winStreakMaxCountTime + "总共最大连盈次数:" + winStreakMaxCount + ",总共最大连亏时间:" + stopStreakMaxCountTime + ",总共最大连亏次数:" + stopStreakMaxCount);

        Double finalAvaliableMoney = yAvaliableMoney.get(yAvaliableMoney.size() - 1);
        double finalWinRatio = 0.0;
        if (transferCounts > 0) {
            finalWinRatio = new BigDecimal(winCounts).divide(new BigDecimal(transferCounts), 3, BigDecimal.ROUND_DOWN).multiply(new BigDecimal("100")).doubleValue();
        }

        BigDecimal initStopMax = (BigDecimal) redisTemplate.opsForValue().get("initStopMax:" + destinationCoins);
        List<BigDecimal> initStopMaxList = (List<BigDecimal>) redisTemplate.opsForValue().get("initStopMaxList:" + destinationCoins);

        BigDecimal bd = new BigDecimal(Double.toString(finalAvaliableMoney));
        bd = bd.setScale(1, RoundingMode.HALF_DOWN);
        finalAvaliableMoney = bd.doubleValue();
        BigDecimal winRatioPer = null;
        BigDecimal lossRatioPer = null;
        int lossCounts = transferCounts - winCounts;
        if (winMoney.doubleValue() != 0 && winCounts != 0 && lossMoney.doubleValue() != 0 && lossCounts != 0) {
            winRatioPer = winMoney.divide(new BigDecimal(winCounts), 6, BigDecimal.ROUND_DOWN).divide(new BigDecimal(openMoney), 6, BigDecimal.ROUND_DOWN);
            lossRatioPer = lossMoney.divide(new BigDecimal(lossCounts), 6, BigDecimal.ROUND_DOWN).divide(new BigDecimal(openMoney), 6, BigDecimal.ROUND_DOWN);
        }
        String chartTitle = destinationCoins + " 开单总数:" + transferCounts + " 盈利次数:" + winCounts + " 总胜率:" + finalWinRatio + "% 最终余额:" + finalAvaliableMoney + " 盈亏比:" + winLossMoneyRatio
                + "\n多单最大连盈时间:" + buyWinStreakMaxCountTime + " 多单最大连盈次数:" + buyWinStreakMaxCount
                + "\n多单最大连亏时间:" + buyStopStreakMaxCountTime + " 多单最大连亏次数:" + buyStopStreakMaxCount
                + "\n空单最大连盈时间:" + sellWinStreakMaxCountTime + " 空单最大连盈次数:" + sellWinStreakMaxCount
                + "\n空单最大连亏时间:" + sellStopStreakMaxCountTime + " 空单最大连亏次数:" + sellStopStreakMaxCount
                + "\n总共最大连盈时间:" + winStreakMaxCountTime + " 总共最大连盈次数:" + winStreakMaxCount
                + "\n总共最大连亏时间:" + stopStreakMaxCountTime + " 总共最大连亏次数:" + stopStreakMaxCount
                + "\n最大止损:" + initStopMax + " 平均盈利率:" + winRatioPer + " 平均亏损率:" + lossRatioPer;
        String fileName = (destinationCoins + " " + fromString + "to" + toString).replaceAll("[^a-zA-Z0-9.-]", "_");
        LineChartUtil.drawDualLineChart(fileName, chartTitle, interval.toString() + "周期", xTime, yWinRate, yAvaliableMoney);
    }

    private QuantBotDetailDTO robotExecute(QuantBotDetailDTO quantBotDetailDTO, BarSeries barSeries, String destinationCoin, String destinationCoins) throws Exception {
        // 定义计算 ATR 的参数
        int atrPeriod = 14;
        BigDecimal inteval = new BigDecimal("0");
        BigDecimal slippage = new BigDecimal("0");

        //均价为0则走择机开仓逻辑,均价大于0则走已经开仓的正常机器人逻辑
        if (new BigDecimal(quantBotDetailDTO.getAvgPrice()).compareTo(BigDecimal.ZERO) == 0) {
            quantBotDetailDTO = robotOpenPositionExecute(quantBotDetailDTO, barSeries, atrPeriod, destinationCoin, inteval, destinationCoins, slippage);
        }

        if (new BigDecimal(quantBotDetailDTO.getAvgPrice()).compareTo(BigDecimal.ZERO) > 0) {
            quantBotDetailDTO = robotRunningExecute(quantBotDetailDTO, barSeries, destinationCoin, inteval);
        }

        return quantBotDetailDTO;
    }

    private QuantBotDetailDTO robotOpenPositionExecute(QuantBotDetailDTO quantBotDetailDTO, BarSeries barSeries, int atrPeriod, String destinationCoin, BigDecimal inteval, String destinationCoins, BigDecimal slippage) throws Exception {
        JSONObject candleStickObjNow = candleStickObjNowMap.get(destinationCoin);
        BigDecimal lastAutoClearTime = new BigDecimal(quantBotDetailDTO.getCurrentAutoClearTime());
        BigDecimal nowTime = new BigDecimal(barSeries.getBar(barSeries.getEndIndex()).getBeginTime().toInstant().toEpochMilli());
        BigDecimal fee = new BigDecimal(quantBotDetailDTO.getInitOrderPosition()).multiply(new BigDecimal("0.0004"));

        if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.BUY.getCode())) {
            if (nowTime.subtract(lastAutoClearTime).compareTo(inteval) >= 0) {
                QuantBotDetailDTO[] dtos = iTechniqueSymbolService4BackTest.judgeTrendBuyBreakThroughOpen(candleStickObjNow, atrPeriod, quantBotDetailDTO, barSeries, destinationCoins, slippage);
                quantBotDetailDTO = dtos[0];
                if (dtos[1] != null) {
                    transferCounts += 1;
                    thisCircleMoneyChange = thisCircleMoneyChange.subtract(new BigDecimal(quantBotDetailDTO.getInitOrderPosition()).divide(leverageRatio)).subtract(fee);
                    quantBotDetailDTO = openPosition(quantBotDetailDTO);
                }
            }
        } else if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.SELL.getCode())) {
            if (nowTime.subtract(lastAutoClearTime).compareTo(inteval) >= 0) {
                QuantBotDetailDTO[] dtos = iTechniqueSymbolService4BackTest.judgeTrendSellBreakThroughOpen(candleStickObjNow, atrPeriod, quantBotDetailDTO, barSeries, destinationCoins, slippage);
                quantBotDetailDTO = dtos[0];
                if (dtos[1] != null) {
                    transferCounts += 1;
                    thisCircleMoneyChange = thisCircleMoneyChange.subtract(new BigDecimal(quantBotDetailDTO.getInitOrderPosition()).divide(leverageRatio)).subtract(fee);
                    quantBotDetailDTO = openPosition(quantBotDetailDTO);
                }
            }
        } else {
            throw new BaseBizException(ResponseStatusEnum.ROBOT_DIRECTION_NOT_EXIST);
        }
        return quantBotDetailDTO;
    }

    private QuantBotDetailDTO robotRunningExecute(QuantBotDetailDTO quantBotDetailDTO, BarSeries barSeries, String destinationCoin, BigDecimal inteval) throws Exception {
        JSONObject candleStickObjNow = candleStickObjNowMap.get(destinationCoin);
        //获取5分钟周期的蜡烛
        List<MyCandleEntry> candleEntries = iTechniqueSymbolService4BackTest.getCandlerStick(candleStickObjNow);
        MyCandleEntry nowCandleEntry = candleEntries.get(candleEntries.size() - 1);
        MyCandleEntry lastCandleEntry = candleEntries.get(candleEntries.size() - 2);

        MyCandleEntry last2CandleEntry = candleEntries.get(candleEntries.size() - 3);
        MyCandleEntry last3CandleEntry = candleEntries.get(candleEntries.size() - 4);
        MyCandleEntry last4CandleEntry = candleEntries.get(candleEntries.size() - 5);
        MyCandleEntry last5CandleEntry = candleEntries.get(candleEntries.size() - 6);
        MyCandleEntry last6CandleEntry = candleEntries.get(candleEntries.size() - 7);
        MyCandleEntry last7CandleEntry = candleEntries.get(candleEntries.size() - 8);
        MyCandleEntry last8CandleEntry = candleEntries.get(candleEntries.size() - 9);

        //当前均价
        BigDecimal avgPrice = new BigDecimal(quantBotDetailDTO.getAvgPrice());
        //BigDecimal openPrice = quantBotDetailDTO.getPinBarCandlerStickDetailDTO().getOpen();

        BigDecimal highRise = null;
        BigDecimal lowRise = null;
        BigDecimal lowPrice = null;

        BigDecimal ratio = null;

        if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.BUY.getCode())) {
            //如果是多单,那么上涨是止盈,下跌是补仓
            lowPrice = new BigDecimal(nowCandleEntry.getLow());
            highRise = (new BigDecimal(nowCandleEntry.getHigh()).subtract(avgPrice)).divide(avgPrice, 6, BigDecimal.ROUND_DOWN);
            lowRise = (new BigDecimal(nowCandleEntry.getLow()).subtract(avgPrice)).divide(avgPrice, 6, BigDecimal.ROUND_DOWN);
            ratio = (new BigDecimal(nowCandleEntry.getOpen()).subtract(avgPrice)).divide(avgPrice, 6, BigDecimal.ROUND_DOWN);
        } else if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.SELL.getCode())) {
            //如果是空单,那么上涨是补仓,下跌是止盈
            lowPrice = new BigDecimal(nowCandleEntry.getHigh());
            highRise = (avgPrice.subtract(new BigDecimal(nowCandleEntry.getLow()))).divide(avgPrice, 6, BigDecimal.ROUND_DOWN);
            lowRise = (avgPrice.subtract(new BigDecimal(nowCandleEntry.getHigh()))).divide(avgPrice, 6, BigDecimal.ROUND_DOWN);
            ratio = (avgPrice.subtract(new BigDecimal(nowCandleEntry.getOpen()))).divide(avgPrice, 6, BigDecimal.ROUND_DOWN);
        } else {
            throw new BaseBizException(ResponseStatusEnum.ROBOT_DIRECTION_NOT_EXIST);
        }

        List<Entry> emas = iCalculatePriceService.calculateEMA(candleStickObjNow, MovingAveragePeriodEnum.MA200);
        BigDecimal lastEmaValue = new BigDecimal((emas.get(emas.size() - 2)).getY());
        BigDecimal last2EmaValue = new BigDecimal((emas.get(emas.size() - 3)).getY());

        BigDecimal nowTime = new BigDecimal(barSeries.getBar(barSeries.getEndIndex()).getEndTime().toInstant().toEpochMilli());

        PinBarCandlerStickDetailDTO pinBarCandlerStickDetailDTO = quantBotDetailDTO.getPinBarCandlerStickDetailDTO();
        BigDecimal initStopLossRatio = pinBarCandlerStickDetailDTO.getStopLossRate();
        BigDecimal originRatio = ratio;

        if (lowRise.compareTo(initStopLossRatio) <= 0) {
            ratio = initStopLossRatio;
        } else if (lowRise.compareTo(SystemParamContants.stop_loss_max.negate()) <= 0) {
            ratio = SystemParamContants.stop_loss_max;
        }
        BigDecimal winRatio = quantBotDetailDTO.getPinBarCandlerStickDetailDTO().getBreakEvenRate().abs();
        if (highRise.compareTo(winRatio) >= 0) {
            ratio = winRatio;
        }

        BigDecimal breakEvenRate = ratio.abs();
        BigDecimal profitWithOutFee = breakEvenRate.multiply(new BigDecimal(quantBotDetailDTO.getInitOrderPosition()));
        BigDecimal stopLossRate = ratio.abs();
        BigDecimal lossWithOutFee = stopLossRate.multiply(new BigDecimal(quantBotDetailDTO.getInitOrderPosition()));
        BigDecimal fee = new BigDecimal(quantBotDetailDTO.getInitOrderPosition()).multiply(new BigDecimal("0.0004"));

        List<Num> atrs = iTechniqueSymbolService4BackTest.getATR(barSeries, 14);
        BigDecimal lastAtr = new BigDecimal(atrs.get(atrs.size() - 2).floatValue());
        //BigDecimal atrChoose = lastAtr.multiply(quantBotDetailDTO.getMultipleAtr());
        BigDecimal atrChoose = lastAtr.multiply(new BigDecimal("0"));
        int stopLossCount = 2;

        int rsiTimeFrame = 2;
        BigDecimal lastRsi = iTechniqueSymbolService4BackTest.getRsi(barSeries, rsiTimeFrame, barSeries.getEndIndex() - 1);

        Long timeDiffer = nowCandleEntry.getOpenTime() - Long.valueOf(quantBotDetailDTO.getCurrentAddTime());
        int diBarCount = 14;
        int adxBarCount = 14;
        double lastAdx = iTechniqueSymbolService4BackTest.getADX(barSeries, diBarCount, adxBarCount, barSeries.getEndIndex() - 1);
        if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.BUY.getCode())) {
            if (/*lastRsi.compareTo(new BigDecimal("50")) >= 0*/
                    (/*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) > 0*/
                            /*|| lastAdx >= 30*/
                            /*originRatio.compareTo(initStopLossRatio.abs()) >= 0*/
                            /*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) < 0*/
                            highRise.compareTo(winRatio) >= 0
                                    /*ratio.compareTo(initStopLossRatio) < 0*/


                                    /*|| new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue120) < 0*/
                                    /*timeDiffer > 0)*/
                                    /*nowCandleEntry.getOpenTime() > Long.valueOf(quantBotDetailDTO.getCurrentAddTime()) &&*/
                     /*&&
                            new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue10) > 0*/
                    /*&& new BigDecimal(last2CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last3CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last4CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last5CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last6CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last7CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last8CandleEntry.getClose()).compareTo(lastEmaValue200) < 0*/

                                    && originRatio.compareTo(BigDecimal.ZERO) < 0) || (lowRise.compareTo(initStopLossRatio) <= 0)
                            || (lowRise.compareTo(SystemParamContants.stop_loss_max.negate()) <= 0)) {
                quantBotDetailDTO = toExecuteAntiRise(quantBotDetailDTO);
                quantBotDetailDTO.setCurrentAutoClearTime(String.valueOf(nowTime));
                quantBotDetailDTO.setMultipleAtr(BigDecimal.ZERO);
                String progressTime = barSeries.getBar(barSeries.getEndIndex()).getBeginTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
                thisCircleMoneyChange = thisCircleMoneyChange.add(new BigDecimal(quantBotDetailDTO.getInitOrderPosition()).divide(leverageRatio)).add(lossWithOutFee.negate().subtract(fee));
                quantBotDetailDTO = getStopStreakCount(quantBotDetailDTO, progressTime);
                if (quantBotDetailDTO.getBuyStopStreakCount() >= stopLossCount && ratio.compareTo(initStopLossRatio) == 0) {
                    quantBotDetailDTO.setCanOpen(false);
                }
                lossMoney = lossMoney.add(lossWithOutFee.abs());
                int lossCounts = transferCounts - winCounts;
                if (winMoney.doubleValue() != 0 && winCounts != 0 && lossMoney.doubleValue() != 0 && lossCounts != 0) {
                    BigDecimal divide = winMoney.divide(new BigDecimal(winCounts), 6, BigDecimal.ROUND_DOWN);
                    BigDecimal divide2 = lossMoney.divide(new BigDecimal(lossCounts), 6, BigDecimal.ROUND_DOWN);
                    winLossMoneyRatio = divide.divide(divide2, 6, BigDecimal.ROUND_DOWN);
                }

                System.out.println(destinationCoin + "多单亏损进度时间: " + progressTime + "总交易次数:" + transferCounts + ",盈利次数:" + winCounts + ",本次亏损率:" + stopLossRate + ",本次亏损金额:" + lossWithOutFee + ",当前盈亏比:" + winLossMoneyRatio);
                System.out.println(destinationCoin + "多单最大连亏时间: " + buyStopStreakMaxCountTime + "多单最大连亏次数:" + buyStopStreakMaxCount + ",多单当前连亏次数:" + quantBotDetailDTO.getBuyStopStreakCount() + ",总最大连亏时间:" + stopStreakMaxCountTime + ",总最大连亏次数:" + stopStreakMaxCount + ",总当前连亏次数:" + stopStreakCount);
                System.out.println("--------------------------------------------------------------------------");
            } else if (
                    (/*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) > 0*/
                            /*|| lastAdx >= 30*/
                            /*originRatio.compareTo(initStopLossRatio.abs()) >= 0*/
                            /*timeDiffer > 0*/
                            /*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) < 0*/
                            highRise.compareTo(winRatio) >= 0
                            /*ratio.compareTo(initStopLossRatio) < 0*/

                            /* new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) < 0*/
                            /*|| new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue120) < 0*/)
                    /*timeDiffer > 0 &&
                            new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue10) > 0*/
                            /*lastRsi.compareTo(new BigDecimal("50")) >= 0*/
                    /*&& new BigDecimal(last2CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last3CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last4CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last5CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last6CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last7CandleEntry.getClose()).compareTo(lastEmaValue200) < 0
                    && new BigDecimal(last8CandleEntry.getClose()).compareTo(lastEmaValue200) < 0*/

                            && originRatio.compareTo(BigDecimal.ZERO) > 0) {
                winCounts += 1;
                quantBotDetailDTO = toExecuteAntiRise(quantBotDetailDTO);
                quantBotDetailDTO.setCurrentAutoClearTime(String.valueOf(nowTime));
                quantBotDetailDTO.setMultipleAtr(BigDecimal.ZERO);
                String progressTime = barSeries.getBar(barSeries.getEndIndex()).getBeginTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
                thisCircleMoneyChange = thisCircleMoneyChange.add(new BigDecimal(quantBotDetailDTO.getInitOrderPosition()).divide(leverageRatio)).add(profitWithOutFee.subtract(fee));
                quantBotDetailDTO = getWinStreakCount(quantBotDetailDTO, progressTime);
                winMoney = winMoney.add(profitWithOutFee.abs());
                int lossCounts = transferCounts - winCounts;
                if (winMoney.doubleValue() != 0 && winCounts != 0 && lossMoney.doubleValue() != 0 && lossCounts != 0) {
                    BigDecimal divide = winMoney.divide(new BigDecimal(winCounts), 6, BigDecimal.ROUND_DOWN);
                    BigDecimal divide2 = lossMoney.divide(new BigDecimal(lossCounts), 6, BigDecimal.ROUND_DOWN);
                    winLossMoneyRatio = divide.divide(divide2, 6, BigDecimal.ROUND_DOWN);
                }
                System.out.println(destinationCoin + "多单盈利进度时间: " + progressTime + "总交易次数:" + transferCounts + ",盈利次数:" + winCounts + ",本次盈利率:" + breakEvenRate + ",本次盈利金额:" + profitWithOutFee + ",当前盈亏比:" + winLossMoneyRatio);
                System.out.println(destinationCoin + "多单最大连盈时间: " + buyWinStreakMaxCountTime + "多单最大连盈次数:" + buyWinStreakMaxCount + ",多单当前连盈次数:" + buyWinStreakCount + ",总最大连盈时间:" + winStreakMaxCountTime + ",总最大连盈次数:" + winStreakMaxCount + ",总当前连盈次数:" + winStreakCount);
                System.out.println("--------------------------------------------------------------------------");
            }
        } else if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.SELL.getCode())) {
            if ((/*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.subtract(atrChoose)) < 0*/
                    (/*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) < 0*/
                            /*|| lastAdx >= 30*/
                            /*originRatio.compareTo(initStopLossRatio.abs()) >= 0*/
                            /*timeDiffer > 0*/
                            /*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) > 0*/
                            highRise.compareTo(winRatio) >= 0
                            /*ratio.compareTo(initStopLossRatio) < 0*/

                            /*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) > 0*/
                            /*|| new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue120) > 0*/)
                    /*timeDiffer > 0 &&
                            new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue10) < 0*/
                            /*lastRsi.compareTo(new BigDecimal("50")) <= 0*/
                    /*&& new BigDecimal(last2CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last3CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last4CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last5CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last6CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last7CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last8CandleEntry.getClose()).compareTo(lastEmaValue200) > 0*/

                            && originRatio.compareTo(BigDecimal.ZERO) < 0) || (lowRise.compareTo(initStopLossRatio) <= 0)
                    || (lowRise.compareTo(SystemParamContants.stop_loss_max.negate()) <= 0)) {
                quantBotDetailDTO = toExecuteAntiRise(quantBotDetailDTO);
                quantBotDetailDTO.setCurrentAutoClearTime(String.valueOf(nowTime));
                quantBotDetailDTO.setMultipleAtr(BigDecimal.ZERO);
                String progressTime = barSeries.getBar(barSeries.getEndIndex()).getBeginTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
                thisCircleMoneyChange = thisCircleMoneyChange.add(new BigDecimal(quantBotDetailDTO.getInitOrderPosition()).divide(leverageRatio)).add(lossWithOutFee.negate().subtract(fee));
                quantBotDetailDTO = getStopStreakCount(quantBotDetailDTO, progressTime);
                if (quantBotDetailDTO.getSellStopStreakCount() >= stopLossCount && ratio.compareTo(initStopLossRatio) == 0) {
                    quantBotDetailDTO.setCanOpen(false);
                }
                lossMoney = lossMoney.add(lossWithOutFee.abs());
                int lossCounts = transferCounts - winCounts;
                if (winMoney.doubleValue() != 0 && winCounts != 0 && lossMoney.doubleValue() != 0 && lossCounts != 0) {
                    BigDecimal divide = winMoney.divide(new BigDecimal(winCounts), 6, BigDecimal.ROUND_DOWN);
                    BigDecimal divide2 = lossMoney.divide(new BigDecimal(lossCounts), 6, BigDecimal.ROUND_DOWN);
                    winLossMoneyRatio = divide.divide(divide2, 6, BigDecimal.ROUND_DOWN);
                }
                System.out.println(destinationCoin + "空单亏损进度时间: " + progressTime + "总交易次数:" + transferCounts + ",盈利次数:" + winCounts + ",本次亏损率:" + stopLossRate + ",本次亏损金额:" + lossWithOutFee + ",当前盈亏比:" + winLossMoneyRatio);
                System.out.println(destinationCoin + "空单最大连亏时间: " + sellStopStreakMaxCountTime + "空单最大连亏次数:" + sellStopStreakMaxCount + ",空单当前连亏次数:" + quantBotDetailDTO.getSellStopStreakCount() + ",总最大连亏时间:" + stopStreakMaxCountTime + ",总最大连亏次数:" + stopStreakMaxCount + ",总当前连亏次数:" + stopStreakCount);
                System.out.println("--------------------------------------------------------------------------");
            } else if (
                /*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.subtract(atrChoose)) > 0*/
                    (/*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) < 0*/
                            /*|| lastAdx >= 30*/
                            /*originRatio.compareTo(initStopLossRatio.abs()) >= 0*/
                            /*timeDiffer > 0*/
                            /*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) > 0*/
                            highRise.compareTo(winRatio) >= 0
                            /*ratio.compareTo(initStopLossRatio) < 0*/

                            /*new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue.add(atrChoose)) > 0*/
                            /*|| new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue120) > 0*/)
                    /*timeDiffer > 0 &&
                            new BigDecimal(lastCandleEntry.getClose()).compareTo(lastEmaValue10) < 0*/
                            /*lastRsi.compareTo(new BigDecimal("50")) <= 0*/
                    /*&& new BigDecimal(last2CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last3CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last4CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last5CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last6CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last7CandleEntry.getClose()).compareTo(lastEmaValue200) > 0
                    && new BigDecimal(last8CandleEntry.getClose()).compareTo(lastEmaValue200) > 0*/

                            && originRatio.compareTo(BigDecimal.ZERO) > 0) {
                winCounts += 1;
                quantBotDetailDTO = toExecuteAntiRise(quantBotDetailDTO);
                quantBotDetailDTO.setCurrentAutoClearTime(String.valueOf(nowTime));
                quantBotDetailDTO.setMultipleAtr(BigDecimal.ZERO);
                String progressTime = barSeries.getBar(barSeries.getEndIndex()).getBeginTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
                thisCircleMoneyChange = thisCircleMoneyChange.add(new BigDecimal(quantBotDetailDTO.getInitOrderPosition()).divide(leverageRatio)).add(profitWithOutFee.subtract(fee));
                quantBotDetailDTO = getWinStreakCount(quantBotDetailDTO, progressTime);
                winMoney = winMoney.add(profitWithOutFee.abs());
                int lossCounts = transferCounts - winCounts;
                if (winMoney.doubleValue() != 0 && winCounts != 0 && lossMoney.doubleValue() != 0 && lossCounts != 0) {
                    BigDecimal divide = winMoney.divide(new BigDecimal(winCounts), 6, BigDecimal.ROUND_DOWN);
                    BigDecimal divide2 = lossMoney.divide(new BigDecimal(lossCounts), 6, BigDecimal.ROUND_DOWN);
                    winLossMoneyRatio = divide.divide(divide2, 6, BigDecimal.ROUND_DOWN);
                }
                System.out.println(destinationCoin + "空单盈利进度时间: " + progressTime + "总交易次数:" + transferCounts + ",盈利次数:" + winCounts + ",本次盈利率:" + breakEvenRate + ",本次盈利金额:" + profitWithOutFee + ",当前盈亏比:" + winLossMoneyRatio);
                System.out.println(destinationCoin + "空单最大连盈时间: " + sellWinStreakMaxCountTime + "空单最大连盈次数:" + sellWinStreakMaxCount + ",空单当前连盈次数:" + sellWinStreakCount + ",总最大连盈时间:" + winStreakMaxCountTime + ",总最大连盈次数:" + winStreakMaxCount + ",总当前连盈次数:" + winStreakCount);
                System.out.println("--------------------------------------------------------------------------");
            }
        } else {
            throw new BaseBizException(ResponseStatusEnum.ROBOT_DIRECTION_NOT_EXIST);
        }

        return quantBotDetailDTO;
    }

    private void getCandleStickNow(int index, String destinationCoin) {
        JSONArray candleStickArrayNow = candleStickArrayNowMap.get(destinationCoin);
        JSONArray candleStickAllTime = candleStickAllTimeMap.get(destinationCoin);
        JSONObject candleStickObjNow = candleStickObjNowMap.get(destinationCoin);

        candleStickArrayNow.remove(0);
        candleStickArrayNow.add(candleStickAllTime.get(index));
        candleStickObjNow.put("candlesticks", candleStickArrayNow);

        candleStickArrayNowMap.put(destinationCoin, candleStickArrayNow);
        candleStickAllTimeMap.put(destinationCoin, candleStickAllTime);
        candleStickObjNowMap.put(destinationCoin, candleStickObjNow);
    }

    private void getCandleStickInit(Long from, CandlestickInterval interval, String destinationCoin) throws Exception {
        QuantBotDetailDTO buyQuantBotDetailDTO = buyQuantBotDetailDTOMap.get(destinationCoin);
        JSONObject candleStickObjNow = feignService.getCandleStick(Integer.valueOf(buyQuantBotDetailDTO.getExchangeType()), buyQuantBotDetailDTO.getUserId(),
                buyQuantBotDetailDTO.getDestinationCoin(), buyQuantBotDetailDTO.getSourceCoin(), interval, null, from, 480);
        JSONArray candleStickArrayNow = candleStickObjNow.getJSONArray("candlesticks");
        candleStickArrayNow.remove(candleStickArrayNow.size() - 1);
        candleStickObjNow.put("candlesticks", candleStickArrayNow);

        candleStickObjNowMap.put(destinationCoin, candleStickObjNow);
        candleStickArrayNowMap.put(destinationCoin, candleStickArrayNow);
    }

    private void getCandleStickAllTime(Long from, Long to, CandlestickInterval interval, String destinationCoin) throws Exception {
        Boolean continueGetCandlerStick = true;
        Long startTime = from;
        int limit = 1500;
        QuantBotDetailDTO buyQuantBotDetailDTO = buyQuantBotDetailDTOMap.get(destinationCoin);
        JSONArray candleStickAllTime = new JSONArray();
        while (continueGetCandlerStick) {
            JSONObject tempObj = feignService.getCandleStick(Integer.valueOf(buyQuantBotDetailDTO.getExchangeType()), buyQuantBotDetailDTO.getUserId(),
                    buyQuantBotDetailDTO.getDestinationCoin(), buyQuantBotDetailDTO.getSourceCoin(), interval, startTime, null, limit);
            JSONArray candlesticks = tempObj.getJSONArray("candlesticks");
            startTime = Long.valueOf(((JSONObject) candlesticks.get(candlesticks.size() - 1)).getString("closeTime")) + 1l;

            for (Object o : candlesticks) {
                JSONObject candlestick = (JSONObject) o;
                Long openTime = Long.valueOf(candlestick.getString("openTime"));
                if (openTime <= to) {
                    candleStickAllTime.add(candlestick);
                } else {
                    continueGetCandlerStick = false;
                    break;
                }
            }
        }

        candleStickAllTimeMap.put(destinationCoin, candleStickAllTime);
    }

    private Long getTimeStamp(String time) {
        long timestamp = -1;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date date = dateFormat.parse(time);
            timestamp = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timestamp;
    }

    private String convertMillisecondsToHMS(long milliseconds) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = new Date(milliseconds);
        return sdf.format(date);
    }

    private void toSetQuantBotDetail(String userId, String destinationCoin, String openMoney) {
        QuantBotDetailDTO buyQuantBotDetailDTO = new QuantBotDetailDTO();
        buyQuantBotDetailDTO.setExchangeType(String.valueOf(ExchangeTypeEnum.BINANCE.getCode()));
        buyQuantBotDetailDTO.setUserId(userId);
        buyQuantBotDetailDTO.setDestinationCoin(destinationCoin);
        buyQuantBotDetailDTO.setSourceCoin(CoinTypeEnum.USDT.getCode());
        buyQuantBotDetailDTO.setDirection(DirectionEnum.BUY.getValue());
        buyQuantBotDetailDTO.setInitOrderPosition(openMoney);
        buyQuantBotDetailDTO.setBuyStopRate("0.06");
        buyQuantBotDetailDTOMap.put(destinationCoin, buyQuantBotDetailDTO);

        QuantBotDetailDTO sellQuantBotDetailDTO = new QuantBotDetailDTO();
        sellQuantBotDetailDTO.setExchangeType(String.valueOf(ExchangeTypeEnum.BINANCE.getCode()));
        sellQuantBotDetailDTO.setUserId(userId);
        sellQuantBotDetailDTO.setDestinationCoin(destinationCoin);
        sellQuantBotDetailDTO.setSourceCoin(CoinTypeEnum.USDT.getCode());
        sellQuantBotDetailDTO.setDirection(DirectionEnum.SELL.getValue());
        sellQuantBotDetailDTO.setInitOrderPosition(openMoney);
        sellQuantBotDetailDTO.setBuyStopRate("0.06");
        sellQuantBotDetailDTOMap.put(destinationCoin, sellQuantBotDetailDTO);
    }

    private QuantBotDetailDTO openPosition(QuantBotDetailDTO quantBotDetailDTO) throws Exception {
        BigDecimal amount = new BigDecimal(quantBotDetailDTO.getInitOrderPosition());
        //更新当前持仓金额
        //quantBotDetailDTO.setPositionAmount(new BigDecimal(quantBotDetailDTO.getPositionAmount()).add(orderDetailResponseVO.getTradeTurnover()).toString());
        //更新当前持仓数量(币计)
        //quantBotDetailDTO.setPositionNum(new BigDecimal(quantBotDetailDTO.getPositionNum()).add(orderDetailResponseVO.getTradeVolumeNum()).toString());
        //更新当前持仓数量(不确定是什么单位,该交易所下单需要什么单位就是什么单位)
        //quantBotDetailDTO.setPositionPiece(new BigDecimal(quantBotDetailDTO.getPositionPiece()).add(orderDetailResponseVO.getTradeVolume()).toString());
        //更新持仓均价
        //quantBotDetailDTO.setAvgPrice(new BigDecimal(quantBotDetailDTO.getPositionAmount()).divide(new BigDecimal(quantBotDetailDTO.getPositionNum()), 6, BigDecimal.ROUND_DOWN).toString());
        //更新开仓时间
        //quantBotDetailDTO.setCurrentAddTime(String.valueOf(System.currentTimeMillis()));
        quantBotDetailDTO.setAvgPrice(String.valueOf(quantBotDetailDTO.getPinBarCandlerStickDetailDTO().getOpen()));
        return quantBotDetailDTO;
    }

    private QuantBotDetailDTO toExecuteAntiRise(QuantBotDetailDTO quantBotDetailDTO) throws Exception {
        quantBotDetailDTO.setHighRise("0");
        quantBotDetailDTO.setBidRise("0");
        quantBotDetailDTO.setAskRise("0");
        quantBotDetailDTO.setCurrentAddCount("0");
        quantBotDetailDTO.setCurrentAntiAddCount("0");
        //更新当前持仓数量(币计)
        quantBotDetailDTO.setPositionNum("0");
        //更新当前持仓数量(不确定是什么单位,该交易所下单需要什么单位就是什么单位)
        quantBotDetailDTO.setPositionPiece("0");
        //更新持仓金额
        quantBotDetailDTO.setPositionAmount("0");
        //更新持仓均价
        quantBotDetailDTO.setAvgPrice("0");
        //开仓时间清零
        quantBotDetailDTO.setCurrentAddTime("0");
        //是否达到过设定止盈涨幅初始化
        quantBotDetailDTO.setAchieveStopWin(false);
        //是否达到过保本涨幅1初始化
        quantBotDetailDTO.setAchieveBreakEven1(false);
        //是否达到过保本涨幅2初始化
        quantBotDetailDTO.setAchieveBreakEven2(false);
        quantBotDetailDTO.setDivergence(false);
        //交叉时间清零
        quantBotDetailDTO.setCrossTime("0");
        //交叉蜡烛对象清空
        quantBotDetailDTO.setQuantCrossCandlerStickDetailDTO(new QuantCrossCandlerStickDetailDTO());
        //pinBar信号时间清零
        quantBotDetailDTO.setPinbarTime("0");
        //pinBar对象清空
        quantBotDetailDTO.setPinBarCandlerStickDetailDTO(new PinBarCandlerStickDetailDTO());

        return quantBotDetailDTO;
    }

    private QuantBotDetailDTO getWinStreakCount(QuantBotDetailDTO quantBotDetailDTO, String progressTime) {
        if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.BUY.getCode())) {
            buyWinStreakCount += 1;
            quantBotDetailDTO.setBuyStopStreakCount(0);
            if (buyWinStreakCount > buyWinStreakMaxCount) {
                buyWinStreakMaxCount = buyWinStreakCount;
                buyWinStreakMaxCountTime = progressTime;
            }
        } else if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.SELL.getCode())) {
            sellWinStreakCount += 1;
            quantBotDetailDTO.setSellStopStreakCount(0);
            if (sellWinStreakCount > sellWinStreakMaxCount) {
                sellWinStreakMaxCount = sellWinStreakCount;
                sellWinStreakMaxCountTime = progressTime;
            }
        }
        winStreakCount += 1;
        stopStreakCount = 0;
        if (winStreakCount > winStreakMaxCount) {
            winStreakMaxCount = winStreakCount;
            winStreakMaxCountTime = progressTime;
        }
        return quantBotDetailDTO;
    }

    private QuantBotDetailDTO getStopStreakCount(QuantBotDetailDTO quantBotDetailDTO, String progressTime) {
        if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.BUY.getCode())) {
            int buyStopStreakCount = quantBotDetailDTO.getBuyStopStreakCount();
            buyStopStreakCount += 1;
            quantBotDetailDTO.setBuyStopStreakCount(buyStopStreakCount);
            buyWinStreakCount = 0;
            if (buyStopStreakCount > buyStopStreakMaxCount) {
                buyStopStreakMaxCount = buyStopStreakCount;
                buyStopStreakMaxCountTime = progressTime;
            }
        } else if (quantBotDetailDTO.getDirection().equals(PositionSideTypeEnum.SELL.getCode())) {
            int sellStopStreakCount = quantBotDetailDTO.getSellStopStreakCount();
            sellStopStreakCount += 1;
            quantBotDetailDTO.setSellStopStreakCount(sellStopStreakCount);
            sellWinStreakCount = 0;
            if (sellStopStreakCount > sellStopStreakMaxCount) {
                sellStopStreakMaxCount = sellStopStreakCount;
                sellStopStreakMaxCountTime = progressTime;
            }
        }
        stopStreakCount += 1;
        winStreakCount = 0;
        if (stopStreakCount > stopStreakMaxCount) {
            stopStreakMaxCount = stopStreakCount;
            stopStreakMaxCountTime = progressTime;
        }
        return quantBotDetailDTO;
    }

}
